var express = require('express');
var router = express.Router();
var bookDao=require('../dao/bookDao');

/* GET users listing. */
router.get('/list', function(req, res, next) {	
	bookDao.getBookList(function(result){		
		res.render('bookList',{title:"图书列表",bookList:result});		

	});
	
});

router.get('/:id/sectionList', function(req, res, next) {
	var	query=req.params;	
	bookDao.getSectionListById(query.id,function(rs){		
		res.render('bookSectionList',{title:"章节列表",bookList:rs});		
	});
	
});

router.get('/section/:id/context', function(req, res, next) {
	var	query=req.params;
	var bookQ={
		id:req.query.bookId,
		index:req.query.index*1+1
	}

	if(query.id==0){
		res.render('error',{message:"最后一章了",error:{status:1,stack:"e"}});
		return;
	}
	
	bookDao.getSectionContextById(query.id,function(rs){
		var sectionText="";
		try{
			sectionText=rs[0].text;
		}catch(e){
			return ;
		}
		bookDao.getSectionContextByBookIdIndex(bookQ.id,bookQ.index,function(rs){			
			if(rs==null||rs.length<1){
				bookDao.updateSectionState(query.id,function(){});		
				res.render('bookSectionContext',{title:"正文内容",text:sectionText,nextId:0,bookQuery:bookQ});
				return;
			}
			bookDao.updateSectionState(query.id,function(){});			
			res.render('bookSectionContext',{title:"正文内容",text:sectionText,nextId:rs[0].id,bookQuery:bookQ});
		});

	});
	
});

router.get('/seach', function(req, res, next) {	
	res.render('bookSeach',{title:"搜索",});
});


module.exports = router;
